package zen2d;


public class Task {
    private Long millis;
    private Function function;
    private long lastTime;

    public Task(int millis, Function function) {
        this.millis = (long) millis;
        this.function = function;
        this.lastTime = System.nanoTime() / 1000000;// 当前毫秒;
    }

    public Task(Function function) {
        this.millis = millis;
        this.function = function;
    }

    public boolean execute() {
        if (millis == null) {
            execute0();
            return true;
        } else {
            long currentTime = System.nanoTime() / 1000000;// 当前毫秒;

            if (millis <= (currentTime - lastTime)) {
                execute0();
                return true;
            } else {
                return false;
            }
        }
    }

    private void execute0() {
        try {
            function.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
